using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MaterialUI
{
	public class ToggleBase : UIBehaviour
	{
		[SerializeField]
		protected float m_AnimationDuration = 0.5f;

		[SerializeField]
		protected Color m_OnColor = Color.black;

		[SerializeField]
		protected Color m_OffColor = Color.black;

		[SerializeField]
		protected Color m_DisabledColor = Color.black;

		[SerializeField]
		protected bool m_ChangeGraphicColor = true;

		[SerializeField]
		protected Color m_GraphicOnColor = Color.black;

		[SerializeField]
		protected Color m_GraphicOffColor = Color.black;

		[SerializeField]
		protected Color m_GraphicDisabledColor = Color.black;

		[SerializeField]
		protected bool m_ChangeRippleColor = true;

		[SerializeField]
		protected Color m_RippleOnColor = Color.black;

		[SerializeField]
		protected Color m_RippleOffColor = Color.black;

		[SerializeField]
		protected internal Graphic m_Graphic;

		[SerializeField]
		protected bool m_ToggleGraphic;

		[SerializeField]
		protected string m_ToggleOnLabel;

		[SerializeField]
		protected string m_ToggleOffLabel;

		[SerializeField]
		protected ImageData m_ToggleOnIcon;

		[SerializeField]
		protected ImageData m_ToggleOffIcon;

		protected MaterialRipple m_MaterialRipple;

		protected Toggle m_Toggle;

		protected CanvasGroup m_CanvasGroup;

		[SerializeField]
		protected bool m_Interactable = true;

		[SerializeField]
		protected ImageData m_Icon;

		[SerializeField]
		protected string m_Label;

		protected VectorImageData m_LastIconVectorImageData;

		protected Sprite m_LastIconSprite;

		protected string m_LastLabelText;

		protected Color m_CurrentColor;

		protected Color m_CurrentGraphicColor;

		protected int m_AnimState;

		protected float m_AnimStartTime;

		protected float m_AnimDeltaTime;

		public float animationDuration
		{
			get
			{
				return m_AnimationDuration;
			}
			set
			{
				m_AnimationDuration = value;
			}
		}

		public Color onColor
		{
			get
			{
				return m_OnColor;
			}
			set
			{
				m_OnColor = value;
			}
		}

		public Color offColor
		{
			get
			{
				return m_OffColor;
			}
			set
			{
				m_OffColor = value;
			}
		}

		public Color disabledColor
		{
			get
			{
				return m_DisabledColor;
			}
			set
			{
				m_DisabledColor = value;
			}
		}

		public bool changeGraphicColor
		{
			get
			{
				return m_ChangeGraphicColor;
			}
			set
			{
				m_ChangeGraphicColor = value;
			}
		}

		public Color graphicOnColor
		{
			get
			{
				return m_GraphicOnColor;
			}
			set
			{
				m_GraphicOnColor = value;
			}
		}

		public Color graphicOffColor
		{
			get
			{
				return m_GraphicOffColor;
			}
			set
			{
				m_GraphicOffColor = value;
			}
		}

		public Color graphicDisabledColor
		{
			get
			{
				return m_GraphicDisabledColor;
			}
			set
			{
				m_GraphicDisabledColor = value;
			}
		}

		public bool changeRippleColor
		{
			get
			{
				return m_ChangeRippleColor;
			}
			set
			{
				m_ChangeRippleColor = value;
			}
		}

		public Color rippleOnColor
		{
			get
			{
				return m_RippleOnColor;
			}
			set
			{
				m_RippleOnColor = value;
			}
		}

		public Color rippleOffColor
		{
			get
			{
				return m_RippleOffColor;
			}
			set
			{
				m_RippleOffColor = value;
			}
		}

		public Graphic graphic
		{
			get
			{
				return m_Graphic;
			}
			set
			{
				m_Graphic = value;
			}
		}

		public bool toggleGraphic
		{
			get
			{
				return m_ToggleGraphic;
			}
			set
			{
				m_ToggleGraphic = value;
			}
		}

		public string toggleOnLabel
		{
			get
			{
				return m_ToggleOnLabel;
			}
			set
			{
				m_ToggleOnLabel = value;
			}
		}

		public string toggleOffLabel
		{
			get
			{
				return m_ToggleOffLabel;
			}
			set
			{
				m_ToggleOffLabel = value;
			}
		}

		public ImageData toggleOnIcon
		{
			get
			{
				return m_ToggleOnIcon;
			}
			set
			{
				m_ToggleOnIcon = value;
			}
		}

		public ImageData toggleOffIcon
		{
			get
			{
				return m_ToggleOffIcon;
			}
			set
			{
				m_ToggleOffIcon = value;
			}
		}

		public MaterialRipple materialRipple
		{
			get
			{
				return m_MaterialRipple;
			}
			set
			{
				m_MaterialRipple = value;
			}
		}

		public Toggle toggle
		{
			get
			{
				if (!m_Toggle)
				{
					m_Toggle = base.gameObject.GetComponent<Toggle>();
				}
				return m_Toggle;
			}
			set
			{
				m_Toggle = value;
			}
		}

		public string labelText
		{
			get
			{
				if (m_Graphic == null)
				{
					return null;
				}
				Text text = m_Graphic as Text;
				if (text != null)
				{
					return text.text;
				}
				return null;
			}
			set
			{
				if (!(m_Graphic == null))
				{
					Text text = m_Graphic as Text;
					if (text != null)
					{
						text.text = value;
					}
				}
			}
		}

		public ImageData icon
		{
			get
			{
				if (m_Graphic == null)
				{
					return null;
				}
				return m_Graphic.GetImageData();
			}
			set
			{
				if (!(m_Graphic == null))
				{
					m_Graphic.SetImage(value);
				}
			}
		}

		private CanvasGroup canvasGroup
		{
			get
			{
				if (m_CanvasGroup == null)
				{
					m_CanvasGroup = base.gameObject.GetAddComponent<CanvasGroup>();
				}
				return m_CanvasGroup;
			}
		}

		public bool interactable
		{
			get
			{
				return m_Interactable;
			}
			set
			{
				m_Interactable = value;
				toggle.interactable = value;
				if (value)
				{
					Enable();
				}
				else
				{
					Disable();
				}
				UpdateGraphicToggleState();
			}
		}

		protected override void OnEnable()
		{
			m_Toggle = base.gameObject.GetComponent<Toggle>();
			materialRipple = base.gameObject.GetComponent<MaterialRipple>();
		}

		protected override void Start()
		{
			base.Start();
			UpdateGraphicToggleState();
		}

		public void Toggle()
		{
			if (m_Toggle.isOn)
			{
				TurnOn();
			}
			else
			{
				TurnOff();
			}
		}

		protected void UpdateGraphicToggleState()
		{
			UpdateIconDataType();
			if (!(m_Graphic == null) && !(m_Toggle == null) && m_ToggleGraphic)
			{
				Type type = m_Graphic.GetType();
				if (type == typeof(Image) || type == typeof(VectorImage))
				{
					m_Graphic.SetImage((!m_Toggle.isOn) ? m_ToggleOffIcon : m_ToggleOnIcon);
				}
				else if (type == typeof(Text))
				{
					((Text)m_Graphic).text = ((!m_Toggle.isOn) ? m_ToggleOffLabel : m_ToggleOnLabel);
				}
			}
		}

		protected void UpdateIconDataType()
		{
			if (!(m_Graphic == null))
			{
				Type type = m_Graphic.GetType();
				if (type == typeof(Image))
				{
					m_ToggleOnIcon.imageDataType = ImageDataType.Sprite;
					m_ToggleOffIcon.imageDataType = ImageDataType.Sprite;
					m_Icon.imageDataType = ImageDataType.Sprite;
				}
				else if (type == typeof(VectorImage))
				{
					m_ToggleOnIcon.imageDataType = ImageDataType.VectorImage;
					m_ToggleOffIcon.imageDataType = ImageDataType.VectorImage;
					m_Icon.imageDataType = ImageDataType.VectorImage;
				}
			}
		}

		public virtual void TurnOn()
		{
			if ((bool)m_Graphic)
			{
				m_CurrentGraphicColor = m_Graphic.color;
			}
			AnimOn();
			m_AnimStartTime = Time.realtimeSinceStartup;
			m_AnimState = 1;
			UpdateGraphicToggleState();
		}

		public virtual void TurnOnInstant()
		{
			if (m_Interactable)
			{
				SetOnColor();
			}
			UpdateGraphicToggleState();
		}

		public virtual void TurnOff()
		{
			if ((bool)m_Graphic)
			{
				m_CurrentGraphicColor = m_Graphic.color;
			}
			AnimOff();
			m_AnimStartTime = Time.realtimeSinceStartup;
			m_AnimState = 2;
			UpdateGraphicToggleState();
		}

		public virtual void TurnOffInstant()
		{
			if (m_Interactable)
			{
				SetOffColor();
			}
			UpdateGraphicToggleState();
		}

		public virtual void Enable()
		{
			if (m_Toggle.isOn)
			{
				SetOnColor();
			}
			else
			{
				SetOffColor();
			}
			canvasGroup.blocksRaycasts = true;
			canvasGroup.interactable = true;
		}

		public virtual void Disable()
		{
			canvasGroup.blocksRaycasts = false;
			canvasGroup.interactable = false;
		}

		private void Update()
		{
			m_AnimDeltaTime = Time.realtimeSinceStartup - m_AnimStartTime;
			if (m_AnimState == 1)
			{
				if (m_AnimDeltaTime <= animationDuration)
				{
					AnimOn();
				}
				else
				{
					AnimOnComplete();
				}
			}
			else if (m_AnimState == 2)
			{
				if (m_AnimDeltaTime <= animationDuration)
				{
					AnimOff();
				}
				else
				{
					AnimOffComplete();
				}
			}
		}

		public virtual void AnimOn()
		{
			if ((bool)m_Graphic && changeGraphicColor)
			{
				m_Graphic.color = Tween.QuintSoftOut(m_CurrentGraphicColor, (!m_Interactable) ? m_GraphicDisabledColor : m_GraphicOnColor, m_AnimDeltaTime, animationDuration);
			}
			if (m_ChangeRippleColor && m_MaterialRipple != null)
			{
				materialRipple.rippleData.Color = m_RippleOnColor;
			}
		}

		public virtual void AnimOnComplete()
		{
			SetOnColor();
			m_AnimState = 0;
		}

		public virtual void AnimOff()
		{
			if ((bool)m_Graphic && m_ChangeGraphicColor)
			{
				m_Graphic.color = Tween.QuintSoftOut(m_CurrentGraphicColor, (!m_Interactable) ? m_GraphicDisabledColor : m_GraphicOffColor, m_AnimDeltaTime, animationDuration * 0.75f);
			}
			if (m_ChangeRippleColor && m_MaterialRipple != null)
			{
				materialRipple.rippleData.Color = m_RippleOffColor;
			}
		}

		public virtual void AnimOffComplete()
		{
			SetOffColor();
			m_AnimState = 0;
		}

		private void SetOnColor()
		{
			if ((bool)m_Graphic && m_ChangeGraphicColor)
			{
				m_Graphic.color = ((!m_Interactable) ? m_GraphicDisabledColor : m_GraphicOnColor);
			}
			if ((bool)materialRipple && m_ChangeRippleColor)
			{
				materialRipple.rippleData.Color = m_RippleOnColor;
			}
		}

		private void SetOffColor()
		{
			if ((bool)m_Graphic && m_ChangeGraphicColor)
			{
				m_Graphic.color = ((!m_Interactable) ? m_GraphicDisabledColor : m_GraphicOffColor);
			}
			if ((bool)materialRipple && m_ChangeRippleColor)
			{
				materialRipple.rippleData.Color = m_RippleOffColor;
			}
		}
	}
}
